package com.chzu.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chzu.pojo.ActivityInfo;
import com.chzu.pojo.vo.ElementSelectVo;
import com.chzu.result.JsonResult;
import com.chzu.service.ActivityInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.realm.JAASRealm;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName ActivityInfoController
 * @Description TODO
 * @Date 2022/02/13 18:37
 */
@RestController
@RequestMapping("/activityInfo")
@Api(tags = "活动信息的接口")
public class ActivityInfoController {

    @Resource
    private ActivityInfoService activityInfoService;

    @ApiOperation("添加活动信息")
    @Transactional(rollbackFor= Exception.class)
    @PostMapping("/")
    public JsonResult<String> addActivity(@RequestBody ActivityInfo activityInfo) {
        activityInfo.setStatus(true);
        activityInfo.setActivityId(UUID.randomUUID().toString());
        activityInfo.setCreateTime(new Date());
        boolean res = activityInfoService.save(activityInfo);
        if (!res) {
            return JsonResult.fail("添加活动信息失败");

        }
        return JsonResult.ok("添加活动信息成功");
    }

    @ApiOperation("删除活动信息")
    @Transactional(rollbackFor= Exception.class)
    @DeleteMapping("/{id}")
    public JsonResult<String> deleteActivity(@PathVariable("id") Long id) {
        boolean res = activityInfoService.deleteActivity(id);
        if (!res) {
            return JsonResult.fail("删除活动信息失败");
        }
        return JsonResult.ok("删除活动信息成功");
    }

    @ApiOperation("修改活动信息")
    @Transactional(rollbackFor= Exception.class)
    @PutMapping("/")
    public JsonResult<String> updateActivity(@RequestBody ActivityInfo activityInfo) {
        boolean res = activityInfoService.updateById(activityInfo);
        if (!res) {
            return JsonResult.fail("修改活动信息失败");
        }
        return JsonResult.ok("修改活动信息成功");
    }

    @ApiOperation("查询所有活动信息")
    @Transactional(rollbackFor= Exception.class)
    @GetMapping("/")
    public JsonResult<List<ActivityInfo>> getActivity() {
        List<ActivityInfo> list = activityInfoService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("获取最新一条数据展示")
    @GetMapping("/getNowOne")
    public JsonResult<ActivityInfo> getNewOneInfoByStartTime() {
        ActivityInfo data = activityInfoService.getNewOneInfoByStartTime();
        return JsonResult.ok(data);
    }

    @ApiOperation("获取所有活动的主题")
    @GetMapping("/getAllTheme")
    public JsonResult<List<ElementSelectVo>> getAllTheme() {
        List<ElementSelectVo> data = activityInfoService.getAllTheme();
        return JsonResult.ok(data);
    }

    @ApiOperation("按照主题名称查寻活动信息")
    @GetMapping("/getAllActivityByTheme/{theme}")
    public JsonResult<ActivityInfo> getAllActivityByTheme(@PathVariable String theme) {
        ActivityInfo data = activityInfoService.getAllActivityByTheme(theme);
        return JsonResult.ok(data);
    }


}
